package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.utils.DateUtils;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;
    @Autowired
    private RoleDao roleDao;

    //根据手机号查询会员
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    //新增会员
    public void add(Member member) {
        if(member.getPassword() != null){
            member.setPassword(MD5Utils.md5(member.getPassword()));
        }
        memberDao.add(member);
    }

    //根据月份统计会员数量
    public List<Integer> findMemberCountByMonth(List<String> month) {
        List<Integer> list = new ArrayList<>();
        for(String m : month){
            m = m + ".31";//格式：2019.04.31
            Integer count = memberDao.findMemberCountBeforeDate(m);
            list.add(count);
        }
        return list;
    }

    @Override
    public Member getMyMessage(String value) {
        return memberDao.getMyMessage(value);
    }

    @Override
    public Boolean updatePassword(Map map, String id) {
        if (map == null || !map.get("newPassword").equals(map.get("quePassword"))){
            return false;
        }
        if (map.get("newPassword") == null ||
                map.get("quePassword") == null ||
                map.get("oldPassword") == null
        ){
            return false;
        }
        //获取前端输入的旧密码加密
        String oldPassword = (String) map.get("oldPassword");
        String qOld = MD5Utils.md5(oldPassword);
        //获取旧密码进行对比
        String old = memberDao.getOldPassword(id);
        if (old == null || !old.equals(qOld)) {
            return false;
        }
        //获取前端输入的新密码
        String str = (String) map.get("newPassword");
        //加密
        String password = MD5Utils.md5(str);
        memberDao.updatePassword(password,id);
        return true;

    }

    @Override
    public Boolean PWLogin(Map map) {
        //判断输入密码是否为空
        String password = (String) map.get("password");
        //密码加密一下
        String md5Password = MD5Utils.md5(password);
        String telephone = (String) map.get("telephone");
        if (password == null || password.length() == 0) {
            return false;
        }
        //判断手机号或密码是否错误
        Member member = memberDao.findByTelephone(telephone);
        if (member == null || !member.getPassword().equals(md5Password)) {
            return false;
        }
        return true;

    }

    @Override
    public void updatePhoneNumber(Map map) {
        memberDao.updatePhoneNumber(map);
    }

    //查询全部会员
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString)  {
        PageHelper.startPage(currentPage,pageSize);
        Page<Member> page = memberDao.selectByCondition(queryString);
        for (Member member : page) {
            Integer memberId = member.getId();
//            String manager = memberDao.findRoleCountByMemberID(memberId);//查询关联的健康师
//                if (manager != null&&manager.length()>0) {
//                    member.setManager(manager);
//            }
            String age = memberDao.findCountAgeByBirthday(memberId);//通过生日计算年龄
            member.setAge(age);
        }
        return new PageResult(page.getTotal(),page.getResult());
    }

   //新增会员档案
    public void addMember(Member member,Integer managerId) {
        try {
            memberDao.add(member);
//            memberDao.setMemberAndManager(managerId);//新增会员时添加绑定健康师
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

//    //查询全部健康管理师
//    public List<Map<String, Object>> findManagerByRole() {
//        List<Map<String, Object>> options = memberDao.findManagerByRole();
//        return options;
//    }

    //删除会员
    public void delete(Integer id) {

        //查询当前检查项是否和检查组关联
        long count = memberDao.findCountByMemberId(id);
        if(count > 0){
            //当前检查项被引用，不能删除
            throw new RuntimeException("当前会员有预约，不能删除");
        }
        memberDao.delete(id);
    }

//    //编辑前查询当前用户绑定的健康师
//    public  String findManagerByMemberId(Integer memberId) {
//        String manager = memberDao.findRoleCountByMemberID(memberId);
//        return manager;
//    }

    //根据id查询用户
    public Member findById(Integer memberId) {
        Member member = memberDao.findById(memberId);
        return member;
    }

    @Override
    public void edit(Member member) {
        try {
            memberDao.edit(member);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void updateById(Member member) {
        memberDao.editMyMessage(member.getId(),member);
    }


}
